﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using TheDarkKnight;
using UnityEditor;
using UnityEngine;
using static TheDarkKnight.TempletObjectBase;

public class TempleteMeshCopy : MonoBehaviour
{
    [Header("复制模板")]
    public TempletObjectBase Templet;

    [Header("复制对象")]
    public TempletObjectBase Target;

    // Start is called before the first frame update
    void Start()
    {
    }

    public void StartToCopy()
    {
        Debug.Log("开始模版复制");
        TempletObjectBase templet = Templet;
        //Target = (TempletObjectBase)ScriptableObject.CreateInstance(templet.TypeName);
        //data.Vectices = templet.Vectices;
        Target.TypeName = templet.TypeName;
        Target.ModelName = templet.ModelName;
        Target.Triangles = templet.Triangles;
        Target.SurfaceMaterial = templet.SurfaceMaterial;
        Target.SectionMaterial = templet.SectionMaterial;
        Target.Vectices = new List<VerticesData>();
        for (int j = 0; j < templet.Vectices.Count; j++)
        {
            VerticesData newVerticeData = new VerticesData();
            newVerticeData.Vertices = (templet.Vectices[j].Vertices);
            newVerticeData.UV = templet.Vectices[j].UV;
            newVerticeData.Normal = templet.Vectices[j].Normal;
            Target.Vectices.Add(newVerticeData);
        }
        AssetDatabase.SaveAssets(); //存储资源
        AssetDatabase.Refresh(); //刷新
        Debug.Log("模版复制结束");
    }

    public void CreateNewOne() {
        Debug.Log("开始模版复制");
        string directoryPath = @"Assets/ScriptableObject/x/";
        string fullDirPath = System.Environment.CurrentDirectory + "/" + directoryPath;
        TempletObjectBase templet = Templet;
        TempletObjectBase newTarget = (TempletObjectBase)ScriptableObject.CreateInstance(templet.TypeName);
        //data.Vectices = templet.Vectices;
        newTarget.TypeName = templet.TypeName;
        newTarget.ModelName = templet.ModelName;
        newTarget.Triangles = templet.Triangles;
        newTarget.SurfaceMaterial = templet.SurfaceMaterial;
        newTarget.SectionMaterial = templet.SectionMaterial;
        newTarget.Vectices = new List<VerticesData>();
        for (int j = 0; j < templet.Vectices.Count; j++)
        {
            VerticesData newVerticeData = new VerticesData();
            newVerticeData.Vertices = (templet.Vectices[j].Vertices);
            newVerticeData.UV = templet.Vectices[j].UV;
            newVerticeData.Normal = templet.Vectices[j].Normal;
            newTarget.Vectices.Add(newVerticeData);
        }
        if (!Directory.Exists(fullDirPath))
        {
            Directory.CreateDirectory(fullDirPath);
        }
        AssetDatabase.CreateAsset(newTarget, directoryPath + newTarget.TypeName + ".asset");
        AssetDatabase.SaveAssets(); //存储资源
        AssetDatabase.Refresh(); //刷新
        Debug.Log("模版复制结束");

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
